package com.yedean.mall.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yedean.mall.api.CommonPage;
import com.yedean.mall.api.CommonResult;
import com.yedean.mall.form.PmsProductCategoryParam;
import com.yedean.mall.dto.PmsProductCategoryWithChildrenItem;
import com.yedean.mall.entity.PmsProductCategory;
import com.yedean.mall.service.IPmsProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类模块Controller
 *
 * @author yedean
 * @date 2022/8/15
 */
@RestController
@RequestMapping("/admin/productCategory")
@Api(tags = "商品分类管理")
public class PmsProductCategoryController {

    @Autowired
    private IPmsProductCategoryService productCategoryService;


    @ApiOperation("添加产品分类")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> create(@Validated @RequestBody PmsProductCategoryParam productCategoryParam) {

        PmsProductCategory category = new PmsProductCategory();
        BeanUtil.copyProperties(productCategoryParam, category);
        productCategoryService.create(category, productCategoryParam.getProductAttributeIdList());
        return CommonResult.success();
    }


    @ApiOperation("修改商品分类")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> update(@PathVariable Long id,
                                  @Validated
                                  @RequestBody PmsProductCategoryParam productCategoryParam) {

        PmsProductCategory category = new PmsProductCategory();
        BeanUtil.copyProperties(productCategoryParam, category);
        productCategoryService.update(category, new UpdateWrapper<PmsProductCategory>().eq("id", id));
        return CommonResult.success();

    }

    @ApiOperation("根据id获取商品分类")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsProductCategory> getItem(@PathVariable Long id) {
        PmsProductCategory productCategory = productCategoryService.getById(id);
        return CommonResult.success(productCategory);
    }

    @ApiOperation("删除商品分类")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> delete(@PathVariable Long id) {
        boolean remove = productCategoryService.removeById(id);
        if (remove) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询商品分类")
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsProductCategory>> getList(@PathVariable Long parentId,
                                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        IPage<PmsProductCategory> pageList = productCategoryService.getList(parentId, pageSize, pageNum);
        return CommonResult.success(CommonPage.resetPage(pageList));
    }

    @ApiOperation("修改导航栏显示状态")
    @RequestMapping(value = "/update/navStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> updateNavStatus(@RequestParam("ids") List<Long> ids, @RequestParam("navStatus") Integer navStatus) {
        productCategoryService.updateShowStatus(ids, navStatus);
        return CommonResult.success();
    }

    @ApiOperation("查询所有一级分类及子分类")
    @RequestMapping(value = "/list/withChildren", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProductCategoryWithChildrenItem>> listWithChildren() {
        List<PmsProductCategoryWithChildrenItem> list = productCategoryService.listWithChildren();
        return CommonResult.success(list);
    }


}
